﻿/**
 * 复制Word插件
 */
(function(E){
//插件自己的iframe dom对象
var dom = null;

E.plugin("PasteWord", {
	
	defaultHtml: '<p>&nbsp;</p>',
	
	config:{
		width: 600,
		position: 'center',
		overlay: true
	},
	
	// 在插件初始化时执行
	init: function( self ){
		self.cache['html'] = '<div class="jqe-plugin-pasteword"></div>';
	},
	
	one: function(panel, self){
		var container = panel.find(".jqe-plugin-pasteword"),
			option = E.get("option"),
			o = {
				id: 'PasteWord',
				contentEditable: true,
				iframeStyle: {width:'99%', height:'99%', margin:'0 5px'},
				contentCssName: option.contentCssName
			};
		self.notice('请将Word当中的内容粘贴到下面的方框里，然后点击确定按钮。');
		var obj = E.iframe(o, container, self.defaultHtml);
		dom = obj.dom;
		obj.win.focus();
	},
	
	callback: function(panel, self){
		dom.body.innerHTML = self.defaultHtml;
	},
	
	get: function( panel, self, target ){
		var content = "", html = "";
		if( dom ){
			html = dom.body.innerHTML;
			//过滤
			content = self.cleanWord(html);
			
			if( /^<p>&nbsp;<\/p>$/i.test(content) ){
				content = "";
			}
		}
		return content;
	},
/*
	cleanWord_bak: function(html){
		//from kissy editor
		// Remove onmouseover and onmouseout events (from MS Word comments effect)
		html = html.replace(/<(\w[^>]*) onmouseover="([^\"]*)"([^>]*)/gi, "<$1$3");
		html = html.replace(/<(\w[^>]*) onmouseout="([^\"]*)"([^>]*)/gi, "<$1$3");

		// The original <Hn> tag send from Word is something like this: <Hn style="margin-top:0px;margin-bottom:0px">
		html = html.replace(/<H(\d)([^>]*)>/gi, "<h$1>");

		// Word likes to insert extra <font> tags, when using MSIE. (Wierd).
		html = html.replace(/<(H\d)><FONT[^>]*>([\s\S]*?)<\/FONT><\/\1>/gi, "<$1>$2<\/$1>");
		html = html.replace(/<(H\d)><EM>([\s\S]*?)<\/EM><\/\1>/gi, "<$1>$2<\/$1>");

		// Remove <meta xx...>
		html = html.replace(/<meta[^>]*>/ig, "");

		// Remove <link rel="xx" href="file:///...">
		html = html.replace(/<link rel="\S+" href="file:[^>]*">/ig, "");

		// Remove <!--[if gte mso 9|10]>...<![endif]-->
		html = html.replace(/<!--\[if gte mso [0-9]{1,2}\]>[\s\S]*?<!\[endif\]-->/ig, "");

		// Remove <style> ...mso...</style>
		html = html.replace(/<style>[\s\S]*?mso[\s\S]*?<\/style>/ig, "");

		// Remove lang="..."
		html = html.replace(/ lang=".+?"/ig, "");

		// Remove <o:p></o:p>
		html = html.replace(/<o:p><\/o:p>/ig, "");

		// Remove class="MsoNormal"
		html = html.replace(/ class="Mso.+?"/ig, "");
		
		
		html = html.replace(/ mso-spacerun: 'yes'/ig, "");
		//html = html.replace(/ class=\w+?/ig, "");
		
		
		// html = html.replace(/<\/?SPAN[^>]*>/gi, "");
		// html = html.replace(/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3");
		// html = html.replace(/<(\w[^>]*) style="([^"]*)"([^>]*)/gi, "<$1$3");
		// html = html.replace(/<(\w[^>]*) lang=([^ |>]*)([^>]*)/gi, "<$1$3");
		// html = html.replace(/<\\?\?xml[^>]*>/gi, "");
		// html = html.replace(/<\/?\w+:[^>]*>/gi, "");
		// html = html.replace(/(&nbsp;)+/gi, " ");

		// html = html.replace(/<(style|xml).*?>[\s\S]*?<\/\1>/gi, '');
		// html = html.replace(/<!--[\s\S]*?-->/gi, '');
		// html = html.replace(/<(link|meta)[^>]*?>/gi, '');
		// html = html.replace(/<img[^>]*?>/gi, '');
		

		return html;
	},
*/
	cleanWord: function(sHtml){
		var cleanPaste=2;
		if( cleanPaste>0
			&& cleanPaste<3
			&& sHtml.match(/mso(-|normal)|WordDocument|<table\s+[^>]*?x:str/i)
		){
			//区块标签清理
			sHtml = sHtml.replace(/<!--[\s\S]*?-->|<!(--)?\[[\s\S]+?\](--)?>|<style(\s+[^>]*?)?>[\s\S]*?<\/style>/ig, '');
			
			sHtml = sHtml.replace(/\r?\n/ig, '');

			sHtml=sHtml.replace(
				/(<(\/?)([\w-:]+))((?:\s+[\w-:]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^>\s]+))?)*)\s*(\/?>)/g,
				function(all,left,end,tag,attr,right){
					tag=tag.toLowerCase();
					if( ( tag.match(/^(link|img)$/) && attr.match(/file:\/\//i) )
						|| tag.match(/:/)
						|| (tag==='span' && cleanPaste===2)
					){
						return '';
					}
					if(!end){
						attr=attr.replace(/\s([\w-:]+)(?:\s*=\s*("[^"]*"|'[^']*'|[^>\s]+))?/ig,function(all,n,v){
							n=n.toLowerCase();
							v=v.match(/^(["']?)(.*)\1/)[2];
							if(n.match(/:/))return '';
							else if(n.match(/^(class|lang|language|span)$/))return '';
							else if(tag==='td'&&(n==='height'||(n==='width'&&!attr.match(/\scolspan="\d+"/i))))return '';
							else if(n==='style'){
								if(cleanPaste===2)return '';
								v=$.trim(v.replace(/\s*(mso-[^:]+:.+?|margin\s*:\s*0cm 0cm 0pt\s*|(text-align|font-variant|line-height)\s*:\s*.+?)(;|$)\s*/ig,''));
								return v?' '+n+'="'+v.replace(/"/g,"'")+'"':'';
							}
							return all;
						})
					}
					return left+attr+right;
				}
			);
			
			for(var i=0;i<3;i++){
				sHtml = sHtml.replace( /<([^\s>]+)(\s+[^>]*)?>\s*<\/\1>/g, function(all,tag){
					if(tag.match(/^a$/i))return all;
					else return '';
				});
			}//空内容的标签
		}
		return sHtml;
	}
	
});

})(jQEditor);